﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class OrderProductApp
    {
        private readonly OrderProductRepository _service = new OrderProductRepository();
        private readonly OrderRepository _orderRepository = new OrderRepository();
        private readonly UserRepository _userservice = new UserRepository();
        private readonly TechnologyRepository _technologyRepository = new TechnologyRepository();

        public User GetSinleUserById(Guid id)
        {
            return _userservice.FindEntity(a => a.Id == id);
        }
        public Order GetSingleOrder(Guid id)
        {
            return _orderRepository.FindEntity(a => a.Id == id);
        }

        public OrderProduct GetSingleOrderProduct(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }

        public OrderProduct GetSingleByOrderAndProduct(Guid orderID,Guid productID)
        {
            var op = _service.FindEntity(a => a.OrderId == orderID && a.ProductId == productID);
            return op;
        }

        public List<OrderProduct> GetList(Guid oid)
        {
            return _service.IQueryable(a => a.OrderId == oid).ToList();
        }

        /// <summary>
        /// 根据加工id
        /// </summary>
        /// <param name="worksheetId"></param>
        /// <returns></returns>
        public OrderProduct GetSingleOrderProductById(Guid worksheetId)
        {
            return _service.FindEntity(a => a.WorksheetId == worksheetId);
        }
        /// <summary>
        /// 根据采购id
        /// </summary>
        /// <param name="purchaseId"></param>
        /// <returns></returns>
        public OrderProduct GetSingleOrderProductBypurId(Guid purchaseId)
        {
            return _service.FindEntity(a => a.PurchaseId == purchaseId);
        }

        public int Add(List<OrderProduct> ls)
        {
            return _service.Insert(ls);
        }

        public int AddTechnology(List<Technology> tcList)
        {
            return _technologyRepository.Insert(tcList);
        }

        public int GetCheckOrderProductByOrderId(Guid orderId)
        {
            return _service.GetCheckOrderProductByOrderId(orderId);
        }

        public int GetCheckTOrderProductByOrderId(Guid orderId)
        {
            return _service.GetCheckTOrderProductByOrderId(orderId);
        }

        public IEnumerable<ProductTechnologyView> GetOrderProductsByOrderId(Guid orderId)
        {
            return _service.GetOrderProductsByOrderId(orderId).OrderByDescending(a => a.CreateTime);
        }
        public ProductTechnologyView GetOrderProductsByOrderProductId(Guid orderProductId)
        {
            return _service.GetOrderProductsByOrderProductId(orderProductId);
        }
        /// <summary>
        /// 获取订单下产品详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<ProductView> GetListByOrderID(Guid id)
        {
            return _service.GetProductListByOrder(id);
        }

        /// <summary>
        /// 获取OrderProduct表数据根据订单号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<OrderProduct> GetOrderProductByOrder(Guid id)
        {
            return _service.IQueryable(a => a.OrderId == id);
        }

        public int Update(OrderProduct op)
        {
            return _service.Update(op);
        }

        public int Add(OrderProduct op)
        {
            return _service.Insert(op);
        }      

    }
}
